/*******************************************************************************
LevelView.cpp

Notes:
*******************************************************************************/

#include "LevelView.h"

// The type of View
const std::string LevelView::Type = "Level";

//	LevelView(...)
//		Initializes a level view
//			entityID - The associated entity
//			entityData - The data for the entity
LevelView::LevelView(EntityID entityID, EntityResource& entityData)
{
	// Store the entity ID
	m_entity = entityID;

	// Load the image from the given file
	m_image.LoadFromFile(entityData.GetProperty("ImageFile"));

	// Set the image as the sprite's image
	m_sprite.SetImage(m_image);

	// Set a level view to the lowest level possible
	m_iLevel = 0;

	// Initialize the font
	m_Font.LoadFromFile("resources/ArcadeClassic.ttf");
	m_String.SetFont(m_Font);
	m_String.SetSize(32);
} // LevelView

//	Render(...)
//		Renders the level view to the window
//			window - The window to render to
void LevelView::Render(sf::RenderWindow& window)
{
	float x, y, w, h;

	// Get a pointer to the associated entity
	LevelEntity* pEntity = static_cast<LevelEntity*>(EntityManager::GetManager()->GetEntity(m_entity));

	// Store information about the entity
	x = pEntity->GetX();
	y = pEntity->GetY();
	w = pEntity->GetWidth();
	h = pEntity->GetHeight();

	// Update the sprite with the new information
	m_sprite.SetX(x);
	m_sprite.SetY(y);
	m_sprite.Resize(w, h);

	// Setup the players' score string
	std::ostringstream strScores;
	strScores << "P1   " << pEntity->GetLeftScore() << "          P2   " << pEntity->GetRightScore();
	m_String.SetText(strScores.str());

	// Move the text into place
	float fStringWidth = m_String.GetRect().Right - m_String.GetRect().Left;
	m_String.SetPosition((w / 2.0f) - (fStringWidth / 2.0f), 8.0f);

	// Draw the sprite to the window
	window.Draw(m_sprite);
	// Draw the scores to the window
	window.Draw(m_String);
} // Render